<template>
  <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
    <el-form-item label="书名" prop="title">
      <el-input v-model="formData.title" />
    </el-form-item>
    <el-form-item label="ISBN" prop="isbn">
      <el-input v-model="formData.isbn" />
    </el-form-item>
    <el-form-item label="出版社" prop="publisher">
      <el-select v-model="formData.publisher" placeholder="请选择出版社">
        <el-option v-for="item in publishers" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleSubmit">提交</el-button>
      <el-button @click="handleReset">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script setup>
import { ref } from 'vue';
// 引入async-validator<-功能：提供图书信息录入的表单界面，包含书名、ISBN、出版社等字段
// ->特性：使用Element Plus的 el-form 组件实现表单验证（通过 async-validator ），支持提交验证和表单重置

const formRef = ref(null);
const formData = ref({ title: '', isbn: '', publisher: '' });
const publishers = ref([/* 出版社数据 */]);

// 定义验证规则（使用async-validator）
const rules = ref({
  title: [{ required: true, message: '书名不能为空', trigger: 'blur' }],
  isbn: [
    { required: true, message: 'ISBN不能为空', trigger: 'blur' },
    { pattern: /^\d{13}$/, message: '请输入13位数字的ISBN-13编码', trigger: 'blur' }
  ],
  publisher: [{ required: true, message: '请选择出版社', trigger: 'change' }]
});

// 提交验证
const handleSubmit = () => {
  formRef.value?.validate((valid) => {
    valid ? console.log('提交数据:', formData.value) : console.log('验证失败');
  });
};

// 重置表单
const handleReset = () => {
  formRef.value?.resetFields();
};
</script>